package com.mobi.Survey.model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mobi.Survey.controller.Pojo;
import com.mobi.Survey.controller.SubscriptionPojo;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * SubscriptionTable.java Table of Subscription data of Survey
 * 
 */
public class SubscriptionTable extends AbstractTable {

	public static final String TABLE = "subscription";

	public static final String COL_ID = "_id";
	public static final String COL_SUBS_ID = "subs_id";
	public static final String COL_SUBS_FNAME = "fname";
	public static final String COL_SUBS_LNAME = "lname";
	public static final String COL_SUBS_EMAIL = "email";

	public SubscriptionTable(Context ctx) {
		super(ctx);
	}

	/**
	 * Single subscription data could belongs to many Surveys so when entering
	 * each survey we check weather we already have a subscription record of
	 * that survey or not
	 * 
	 * @param subsId
	 *            Id of Survey against which check
	 * @return true if exist
	 */
	public boolean isNewSubscription(long subsId) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		boolean is = true;
		Cursor cursor = db.query(TABLE, new String[] { COL_SUBS_ID },
				COL_SUBS_ID + "=? ", new String[] { subsId + "" }, null, null,
				null);
		if (cursor != null) {

			if (cursor.moveToFirst() && cursor.getCount() > 0)
				is = false;
			cursor.close();
			cursor = null;
		}

		return is;

	}

	public SubscriptionTable() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public long insertRow(Pojo obj) {

		SubscriptionPojo subscription = (SubscriptionPojo) obj;

		SQLiteDatabase db = dbHelper.getWritableDatabase();

		ContentValues cv = new ContentValues();

		cv.put(COL_SUBS_ID, subscription.getSubsId());
		cv.put(COL_SUBS_FNAME, subscription.getfName());

		cv.put(COL_SUBS_LNAME, subscription.getlName());

		cv.put(COL_SUBS_EMAIL, subscription.getEmail());

		long id = db.insert(TABLE, null, cv);
		return id;

	}

	@Override
	public boolean createTable(SQLiteDatabase sqliteDb) {
		String query = "create table " + TABLE + "( " + COL_ID
				+ " integer primary key autoincrement, " + COL_SUBS_ID
				+ " BIGINT unique,  " + COL_SUBS_FNAME + " text, "
				+ COL_SUBS_LNAME + " text, " + COL_SUBS_EMAIL + " text)";
		sqliteDb.execSQL(query);
		printMessage(query);
		return true;
	}

	public SubscriptionPojo getSubscriberData(int subsId) {
		SubscriptionPojo sub = new SubscriptionPojo();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		// String query = "SELECT * FROM " + TABLE + " where " + COL_SUBS_ID
		// + "='?' ";

		Cursor cursor = db.query(TABLE, null, COL_SUBS_ID + " = ? ",
				new String[] { subsId + "" }, null, null, null);
		// Cursor cursor = db.rawQuery(query, new String[] { subsId + "" });

		if (cursor != null) {

			if (cursor.moveToFirst()) {

				do {
					sub.setSubsId(cursor.getInt(1));

					sub.setfName(cursor.getString(2));
					sub.setlName(cursor.getString(3));
					sub.setEmail(cursor.getString(4));

				} while (cursor.moveToNext());

			}

			cursor.close();
			cursor = null;
		}

		return sub;
	}

	@Override
	public boolean deleteRow(long id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int ret = db.delete(TABLE, COL_SUBS_ID + " = ? ", new String[] { id
				+ "" });
		return ret > 0;
	}

	@Override
	public void deleteAllData() {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete(TABLE, null, null);

	}

}
